<?php
/**
 * DbUtil.class.php
 *
 * Class for handling database connections/queries
 *
 * @author Klaus
 */

class DbUtil {
	var $db_host;
	var $db_name;
	var $db_user;
	var $db_password;
	var $link;
	var $connections = 0;
	var $queries;
	
    /**
     * Connects to MySQL database server with info retrieved from db_config.inc.php
     */ 
	function connect() {
		// connect to mysql server
		require "db_config.inc.php";
		$this->link = mysql_connect($db_host, $db_user, $db_password) or die("Could not connect to mysql server: " . mysql_error()); 
		
		// connect to the database
        mysql_select_db($db_name) or die("Database: database not found"); 
        
        $this->connections++;
	}
	
    /**
     * Executes query
     *
     * @param string $sql SQL statement to execute
     * @return array result of SQL query
     */ 
	function query($sql) {
		if (!isset($this->link)) {
        		$this->connect();
        	}
        $result = mysql_query($sql,$this->link) or die(mysql_error());
        $this->queries++;
        return $result;	
	}
	
    function fetch_array($result) { 
        $row = mysql_fetch_array($result); 
        // return the array $row or false if none found
        return $row;
    } 
    
    /**
     * Close database connection
     */ 
    function disconnect() {
        mysql_close();
    }
}
?>